<?php

class Generalfunction {

    public function __construct() {
        $CI = & get_instance();
        $CI->load->database();
    }

    protected function isLogin() {
        return true;
    }

    private function siteURL() {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        return $protocol;
    }

    public function getDir() {
        /* $site = array();
        $url = $this->siteURL() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $site['url'] = substr($url, 0, strpos($url, 'PortingCPanel/') + 32);
        $dir = dirname(__FILE__);
        $dir = str_replace("libraries", "views", $dir);
        $site['dir'] = str_replace("\\", "/", $dir) . "/";
        return $site; */
		return 'please use this "base_url()" ';
    }

    public function getKode($path) {
        global $page, $mode;
        //print_r($_SERVER);
        $arr = array();
        $load = new LoadPage($path);
        $arr['page'] = "depan";
        $arr['mode'] = "index";
        if ($load->get_page() != "")
            $arr['page'] = $load->get_page();
        if ($load->get_mode() != "")
            $arr['mode'] = $load->get_mode();

        $arr['code'] = $load->get_code();
        return $arr;
    }

    public function getMenu() {
        $CI = & get_instance();
        $data = array();
        $data['site'] = $this->getDir();
        $auth = new UserAuth();
        $data['userID'] = $auth->getUserID();
        $result = $CI->db->query("select * from mt_menu where id_menu_induk is NULL AND is_active='1' ORDER BY urutan");
        $resultSet = $result->result();
        $data['namamenu'] = array();
        $i = 1;
        foreach ($resultSet as $rowInduk) {
            $data['kodeinduk'][$rowInduk->id_menu] = $rowInduk->id_menu;
            $data['kodeinduk'][$rowInduk->menu] = $rowInduk->menu;
            $data['namamenu'][] = $rowInduk->menu;
            $url = $rowInduk->link;
            $url = str_replace("{root}", $data['site']['url'], $url);
            $data['kodeinduk']['link' . $i] = str_replace("{userid}", $data['userID'], $url);
            $rsResult = $CI->db->query("select * from mt_menu where id_menu_induk = " . trim($data['kodeinduk'][$rowInduk->id_menu]) . " AND is_active='1' ORDER BY urutan ");
            $data['resultMenu'][$rowInduk->menu] = $rsResult->result();
            $data['havechild'][$rowInduk->menu] = $rsResult->num_rows();
            $i++;
        }
        return $data;
    }

    public function generatebreadcrumb($isi) {
        $delim = "&nbsp;&#8250;&nbsp;";
        $out = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width:100%\">";
        $out .= "<tr>";
        $out .= "<td id=\"breadcrumb\" style='margin-left:17px'>";
        $i = 0;
        foreach ($isi as $key => $value) {
            $out .= $i > 0 ? $delim : "";
            $out .= "<a href=\"" . $value . "\" >" . $key . "</a>";
            $i++;
        }
        $out .= "</td>";
        $out .= "</tr>";
        $out .="</table>";
        return $out;
    }

   /*  public function loadMenu($page, $data = array()) {
        $CI = & get_instance();
        $datamenu = $this->getMenu();
        if ($this->isLogin()) {
            $CI->load->view('template/header', $datamenu);
            $CI->load->view('template/menu', $datamenu);
            $CI->load->view($page, $data);
            $CI->load->view('template/footer');
        } else {
            $CI->load->view('login');
        }
    } */

    public function loadMenuNoHeader($menu) {
        $this->initVar();
        $this->data_config['kode'] = $this->getKode();
        $this->load->view('/template/header', $this->data_config);
        if ($this->isLogin()) {
            $this->load->view($menu, $this->data_config);
        } else {
            $this->load->view('login', $this->data_config);
        }
    }

    public function loadMenuHeaderPopup($menu) {
        $data = array();
        $data['site'] = $this->getDir();
        $CI = & get_instance();
        $CI->load->view('/template/header_popup', $data);
        if ($this->isLogin()) {
            $CI->load->view($menu);
        } else {
            $CI->load->view('login');
        }
    }

}

?>
